package com.briup.jd2511.web03.controller;

import com.briup.jd2511.web03.entity.Book;
import com.briup.jd2511.web03.util.Result;
import com.briup.jd2511.web03.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/book")
public class BookController {

    // 查询单个
    @GetMapping("/{id}")
    public Result getBookById(@PathVariable Integer id){
        Book book = new Book();
        book.setId(id);
        book.setName("朝花夕拾");
        int a = 10/0;
        book.setPrice(38.8);
        return Result.ok(book);
    }

    // 查询全部
    @GetMapping
    public Result getAll(){
        Book book1 = new Book(2,"三国演义");
        Book book2 = new Book(3,"红楼梦");
        Book book3 = new Book(4,"哈利·波特");
        List<Book> books = new ArrayList<>();
        Collections.addAll(books,book1,book2,book3);
        return Result.ok(books);
    }

    // 新增
    @PostMapping
    public Result saveBook(@RequestBody Book book){
        System.out.println(book);
        return Result.ok("新增成功");
    }

    // 修改
    @PutMapping
    public Result editBook(@RequestBody Book book){
        System.out.println(book);
        return Result.ok("修改成功");
    }

    // 删除单个
    @DeleteMapping("/{id}")
    public Result removeBookById(@PathVariable Integer id){
        return Result.ok("删除成功，删除的书籍id为"+id);
    }
    // 删除多个
    @DeleteMapping("/deleteBatch")
    public Result removeBookBatch(@RequestBody List<Integer> ids){
        return Result.ok("删除成功，删除的书籍组id为"+ ids);
    }

    // 请求错误情况
    @GetMapping("/error")
    public Result getError(){
        return Result.error(ResultCode.USER_NOT_EXIST);
    }
}
